package com.gdcvi.scan;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 游戏服务端
 */
public class GameServer implements HandConnection{
    public GameServer(int port)throws Exception {
        svrSck = new ServerSocket(port);
        sck =   svrSck.accept();
    }

    private Socket sck ;
    private ServerSocket svrSck;
    private int port;
    @Override
    public void send(Hand hand) {
        try {
            Field field =  hand.getClass().getField("编号");
            int num = field.getInt(hand);
            sck.getOutputStream().write(num);
        }catch (Exception e){
            throw  new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Hand income() {
        try {
            InputStream input =  sck.getInputStream();
            int hand = input.read();
            return   new PalyFactory().createHand(hand);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public void close() {
        try {
            sck.close();
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
}
